﻿(function () {
    "use strict";
    var nav = WinJS.Navigation;
    WinJS.UI.Pages.define("/pages/home/home.html", {

        ready: function (element, options) {

            ClassExplorer.Services.populateClasses().then(function () {
                var mylist = new WinJS.Binding.List(ClassExplorer.Data.Subjects.getSubjects());
                var myGroupedList = mylist.createGrouped(getGroupKey, getGroupData, compareGroups);
                var listView = element.querySelector(".groupeditemslist").winControl;
                listView.itemDataSource = myGroupedList.dataSource;
                listView.groupDataSource = myGroupedList.groups.dataSource;
                listView.oniteminvoked = itemInvokedHandeler;
            });

            // Function used to sort the groups by first letter
            function compareGroups(left, right) {
                return left.toUpperCase().charCodeAt(0) - right.toUpperCase().charCodeAt(0);
            }

            // Function which returns the group key that an item belongs to
            function getGroupKey(dataItem) {
                return dataItem.subject.toUpperCase().charAt(0);
            }

            // Function which returns the data for a group
            function getGroupData(dataItem) {
                return {
                    title: dataItem.subject.toUpperCase().charAt(0)
                };
            }

            function itemInvokedHandeler(args) {
                var item = ClassExplorer.Data.Subjects.getSubjects()[args.detail.itemIndex];
                nav.navigate("/pages/departmentView/departmentView.html", { item: item });
            }

        }
    });
})();
